package edu.xust.MetadataGenerator.jr.service;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

//import edu.xust.MetadataGenerator.domain.Lines;
//import edu.xust.MetadataGenerator.domain.MetaData;
//import edu.xust.MetadataGenerator.jr.service.JRHelperTest;
//import edu.xust.MetadataGenerator.service.FileHelper;
//import net.sf.jasperreports.engine.JRDataSource;
//import net.sf.jasperreports.engine.JasperFillManager;
//import net.sf.jasperreports.engine.JasperPrint;
//import net.sf.jasperreports.engine.data.JRBeanArrayDataSource;
//import net.sf.jasperreports.engine.data.JRMapArrayDataSource;
//import net.sf.jasperreports.engine.export.ooxml.JRDocxExporter;
//import net.sf.jasperreports.engine.util.JRLoader;
//import net.sf.jasperreports.export.OutputStreamExporterOutput;
//import net.sf.jasperreports.export.SimpleDocxExporterConfiguration;
//import net.sf.jasperreports.export.SimpleExporterInput;
//import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
//import net.sf.jasperreports.export.SimpleWriterExporterOutput;

public class JRHelper {
//	public static final int MAX_LINES = 7000;
//	private final static Logger logger = LogManager.getLogger(JRHelper.class);
//
//	/**
//	 * Flat data from dataSource into memory.
//	 * @param jrt_root Location of Jasper report template file which used by subreport.
//	 * @param template Jasper report template file.
//	 * @param dataSource
//	 * @return
//	 */
//	public static ByteArrayOutputStream flatJR2Mem(String template, JRDataSource dataSource, Map<String, Object> parameters) {
//		ByteArrayOutputStream memPrint = new ByteArrayOutputStream();
//		File templateFile = new File(template);
//		try {
//			InputStream templateStream = new FileInputStream(templateFile);
//			JasperFillManager.fillReportToStream(templateStream, memPrint, parameters, dataSource);
//		} catch (Exception e) {
//			logger.error(e.getMessage());
//		}
//		return memPrint;
//	}
//	
//	/**
//	 * Utility function for data model generation.
//	 * @param inputRoot The location of jrt.
//	 * @param outputRoot Target location for output.
//	 * @param md Meta data.
//	 * @return
//	 */
//	public static boolean exportMD2Docx(String inputRoot, String outputRoot, MetaData md){
//		MetaData[] mds = new MetaData[1];
//		mds[0] = md;
//		JRDataSource dataSource = new JRBeanArrayDataSource(mds);
//
//		String jrt_root = FileHelper.removeSlash(inputRoot) + "/jrt";
//		String template = jrt_root + "/DataModel.jasper";
//		String target = FileHelper.removeSlash(outputRoot) + "/DataModel.doc";
//
//		Map<String, Object> parameters = new HashMap<String, Object>();
//		parameters.put("JRT_ROOT", FileHelper.convertSlash(FileHelper.removeSlash(jrt_root)));
//		return export2Docx(FileHelper.convertSlash(target), FileHelper.convertSlash(template), dataSource, parameters);
//	}
//	
//	public static boolean exportSource2Docx(String artifactid_zh, String templateRoot, String sourceRoot, String outputRoot){
//		String jrt_root = FileHelper.removeSlash(templateRoot) + "/jrt";
//		String template = jrt_root + "/Source.jasper";
//		String target = FileHelper.removeSlash(outputRoot) + "/Source.doc";
//
//		JRDataSource dataSource = new JRMapArrayDataSource(JRHelper.loadSource(sourceRoot, MAX_LINES));
//		
//		Map<String, Object> parameters = new HashMap<String, Object>();
//		parameters.put("ARTIFACT_ID", artifactid_zh);
//
//		return export2Docx(FileHelper.convertSlash(target), FileHelper.convertSlash(template), dataSource, parameters);
//	}
//
//	/**
//	 * Export data as docx file.
//	 * @param jrt_root Location of Jasper report template file which used by subreport.
//	 * @param target Docx format file.
//	 * @param template Jasper report template file.
//	 * @param dataSource
//	 */
//	public static boolean export2Docx(String target, String template, JRDataSource dataSource, Map<String, Object> parameters){
//		boolean result = false;
//		try {
//			long start = System.currentTimeMillis();
//
//			ByteArrayOutputStream content = flatJR2Mem(template, dataSource, parameters);
//			JasperPrint print = JRLoader.loadJasperPrint(new ByteArrayInputStream(content.toByteArray()), null);
//			List<JasperPrint> jasperPrints = new ArrayList<JasperPrint>();
//			jasperPrints.add(print);
//
//			JRDocxExporter docxExporter = new JRDocxExporter();
//			docxExporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrints));
//			docxExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(target));
//			logger.debug("Exporting report...");
//			docxExporter.exportReport();
//			result = true;
//			logger.debug("Done!:" + (System.currentTimeMillis() - start));
//		} catch (Exception e) {
//			logger.error(e.getMessage());
//		}
//		
//		return result;
//	}
//
//	/**
//	 * Scan java source file under the specified directory recursively and put
//	 * code of each file into a File/Source pair. 
//	 * @param path
//	 * @return
//	 */
//	public static List<Map<String, Object>> scanSource(String path, int maxLines) {
//		String ext = "java";
//		List<Map<String, Object>> sources = new ArrayList<Map<String, Object>>();
//
//		File dir = new File(path);
//		if (dir.exists() && dir.isDirectory()) {
//
//			File[] files = dir.listFiles();
//			for (File file : files) {
//				if (file.isFile() && ext.equals(FileHelper.getExt(file.getName()))) {
//					Map<String, Object> source = new HashMap<String, Object>();
//					source.put("File", file.getName());
//					if(lineCount > maxLines){
//						break;
//					}
//					Lines lines = FileHelper.readLines(file);
//					lineCount += lines.getCount();
//					source.put("Source", lines.getLines());
//					logger.debug("Read lines:" + lineCount);
//					sources.add(source);
//				} else if(file.isDirectory()){
//					// Scan sub directory.
//					List<Map<String, Object>> subSources = scanSource(file.getAbsolutePath(), maxLines);
//					sources.addAll(subSources);
//				}
//			}
//		}
//
//		return sources;
//	}
//
//	private static int lineCount;
//	public static Map[] loadSource(String path, int maxLines) {
//		lineCount = 0;
//		List<Map<String, Object>> sources = scanSource(path, maxLines);
//		logger.debug(sources.size());
//		return sources.toArray(new Map[0]);
//	}

}